package com.cn.lyl.dao.impl;

import com.cn.lyl.dao.NewsTypeDao;
import com.cn.lyl.model.NewsTypeModel;
import com.cn.lyl.util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NewsTypeDaoImpl implements NewsTypeDao {
    @Override
    public int add(NewsTypeModel NewsTypeModel) {
        return 0;
    }

    @Override
    public int update(NewsTypeModel NewsTypeModel) {
        return 0;
    }

    @Override
    public int delete(NewsTypeModel NewsTypeModel) {
        return 0;
    }

    @Override
    public NewsTypeModel findById(int id) {
        return null;
    }

    @Override
    public List<NewsTypeModel> findAll(NewsTypeModel NewsTypeModel) {
        return null;
    }

    @Override
    public List<NewsTypeModel> findAll() {
        String sql ="select type from news_type";
        ResultSet query = JDBC.query(sql);
        List<NewsTypeModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                NewsTypeModel newsTypeModel = new NewsTypeModel();
                newsTypeModel.setType(query.getString("type"));
                list.add(newsTypeModel);
            }
        }catch (Exception e){

        }
        return list;
    }


    @Override
    public NewsTypeModel findByName(NewsTypeModel newsTypeModel) {
        String sql ="select id from news_type where type ='"+newsTypeModel.getType()+"'";
        ResultSet query = JDBC.query(sql);
        NewsTypeModel newsTypeModel1 = new NewsTypeModel();
        try{
            if(query.next()){
                newsTypeModel1.setId(query.getInt("id"));
                newsTypeModel1.setType(query.getString("type"));
            }
        }catch (Exception e){

        }
        return newsTypeModel1;
    }
}
